package com.walter.myweb.controller;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.walter.common.utils.GetIpAddress;
import com.walter.myweb.pojo.Contents;
import com.walter.myweb.pojo.Tags;
import com.walter.myweb.service.ContentService;
import com.walter.myweb.service.TagService;

import jdk.nashorn.internal.runtime.regexp.joni.Regex;


@Controller
public class myWebController {

	@Autowired
	private TagService tagService;

	@Autowired
	private ContentService contentService;

	// 入口
	@RequestMapping(value = "/")
	public String index(Model model,HttpServletRequest request) {
		System.out.println(GetIpAddress.getIpAddress(request));
		System.out.println(request.getRemoteAddr());
		List<Contents> contents = null ;
		contents = contentService.selectContents();

		List<Tags> tags = tagService.selectTagsList();
		model.addAttribute("tags", tags);
		model.addAttribute("contents", contents);
		

		return "index";
	}

	// 增加标签
	@RequestMapping(value = "/addTag")
	public String addTag(Model model,String tagContent) {
		
		
		Tags tag=new Tags();
		tag.setName(tagContent);
		tagService.addTag(tag);
		
		List<Tags> tags = tagService.selectTagsList();
		model.addAttribute("tags", tags);

		List<Contents> contents = contentService.selectContents();
		model.addAttribute("contents", contents);

		return "index";
	}
	
	
	/**
	 * 根据tag_id添加文章
	 * @param model 返回的视图
	 * @param tagId 相应的标签id
	 * @param richContent 在富文本框里输入的内容
	 * @return 重定向回主页面
	 */
	@RequestMapping(value="/addContentByTagID")
	public String addContentByTagID(Model model,int tagId,String richContent){
		
		//不会处理<p>等
		Contents content=new Contents();
		content.setTag_id(tagId);
		content.setContent(richContent);
		contentService.addContentByTagId(content);
		
		//重定向回主页
		return "redirect:/";
	}

	/**
	 * 查询满足条件的所有内容(此时只会显示有内容的标签)
	 * @param model
	 * @param condition 条件
	 */
	@RequestMapping(value = "/serachByCondition")
	public String serachByCondition(Model model,HttpServletRequest request){
		String condition=request.getParameter("condition");

		List<Contents> contents = null ;
		List<Integer> list=new ArrayList<>();
		
		contents = contentService.selectContents(condition);
		if (contents.isEmpty()) {
			return "redirect:/";
		}
		
		
		List<Tags> tags=null;
		
		//获取内容的所有tagID
		for (Contents content : contents) {
			list.add(content.getTag_id());
		}
		
		//去重并按自然顺序排序
		List<Integer> listNew=new ArrayList<>(new TreeSet<Integer>(list));
		
		tags=tagService.selectTagsListByIDS(listNew);
		
		model.addAttribute("tags", tags);
		model.addAttribute("contents", contents);
		
		return "searchIndex";
	}
	
	/**
	 * 根据id和内容修改
	 * @param model
	 * @param contentId id
	 * @param richContentC 修改内容
	 * @return
	 */
	@RequestMapping(value="/editContentByID")
	public String editContentById(Model model,int contentId,String richContentC){
		
		//不会处理<p>等
		Contents content=new Contents();
		content.setId(contentId);
		content.setContent(richContentC);
		
		contentService.updateContentById(content);
		
		//重定向回主页
		return "redirect:/";
	}
	
	/**
	 * 根据id删除内容(实际上是修改status)
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deleteContentByID")
	public String deleteContentByID(Model model,HttpServletRequest request){
		String id0=request.getParameter("id");
		int id=Integer.parseInt(id0);
		//不会处理<p>等
		Contents content=new Contents();
		content.setId(id);
		
		contentService.deleteContentByID(content);;
		
		//重定向回主页
		return "redirect:/";
	}
}
